package client;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import org.apache.cxf.interceptor.LoggingInInterceptor;
import org.apache.cxf.interceptor.LoggingOutInterceptor;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;

import com.landray.kmss.km.plmtwo.webservice.IKmPlmtwoMainWebServiceService;

import util.AddSoapHeader;
import util.WebServiceConfig;

public class Plm2Client {
	public static final String ADDRESS = "http://oatest.desaysv.com:8080/sys/webservice/kmPlmtwoMainWebserviceService";
	public static final String SERVICENAME = "com.landray.kmss.km.plmtwo.webservice.IKmPlmtwoMainWebServiceService";
	//流程1
	public static final String SYSYTEM_PLM = "PLM";
	public static final String PLM_2 = "plm2.xml";
	public static final String PLM_2_METHOD = "saveXML";

	public static final String PLM_REVIEW_1 = "plm_review_1.xml";
	public static final String PLM_REVIEW_2 = "plm_review_2.xml";
	public static final String PLM_REVIEW_3 = "plm_review_3.xml";

	/**
	 * 主方法
	 *
	 * @throws Exception
	 */
	public static void main(String[] args) throws Exception {
		String XML = (String) readFile("plm2.xml");
		System.out.println(XML);
		WebServiceConfig cfg = WebServiceConfig.getInstance();
		IKmPlmtwoMainWebServiceService service = (IKmPlmtwoMainWebServiceService) callService(ADDRESS, Class.forName(SERVICENAME));
		String result = "";
		//result = service.toYesV("PLM", "DealStatus", "delete,XnS1LYEUIbkW8B");
		result = service.toYesV(SYSYTEM_PLM, "saveXML", XML);
		System.out.println(result);
	}

	/**
	 * 调用服务，生成客户端的服务代理
	 *
	 * @param address      WebService的URL
	 * @param serviceClass 服务接口全名
	 * @return 服务代理对象
	 * @throws Exception
	 */
	public static Object callService(String address, Class serviceClass)
			throws Exception {
		JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
		// 记录入站消息
		factory.getInInterceptors().add(new LoggingInInterceptor());
		// 记录出站消息
		factory.getOutInterceptors().add(new LoggingOutInterceptor());
		// 添加消息头验证信息。如果服务端要求验证用户密码，请加入此段代码
		factory.getOutInterceptors().add(new AddSoapHeader());
		factory.setServiceClass(serviceClass);
		factory.setAddress(address);
		// 使用MTOM编码处理消息。如果需要在消息中传输文档附件等二进制内容，请加入此段代码
		// Map<String, Object> props = new HashMap<String, Object>();
		// props.put("mtom-enabled", Boolean.TRUE);
		// factory.setProperties(props);
		// 创建服务代理并返回
		return factory.create();
	}
	public static Object readFile(String fileName) {
		Object obj = null;
		String path = Plm2Client.class.getClassLoader().getResource("").toString() + fileName;
		path = path.replace("file:/", "/");
		path = path.replace("/", "//");
		File file = new File(path);
		StringBuilder sb = new StringBuilder("");
		try {
			BufferedReader br = new BufferedReader(new FileReader(file));
			String l = null;
			while ((l = br.readLine()) != null) {
				sb = sb.append(l);
			}
			return sb.toString();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return obj;
	}
}
